package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.ArrayField;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataChildren extends ArrayField implements IArray{
	public static String DEFAULT_ID="_Children";

		private GrfxMLEngine engine;
		DataNode parent;

		DataPoint _bounds = new DataPoint(TagModifier.POINT_TYPE_BOUNDS);

	public DataChildren(GrfxMLEngine engine) {
		this(engine,null);
	}
public DataChildren(GrfxMLEngine engine,DataNode parent) {

	this.parent = parent;
	this.engine = engine;
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void add(DataNode data,Object source) throws LockException
{
	_add(data,source);
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}



	XmlElement element;



	for(int loop=0;loop<xml.getElementCount();loop++)
	{
	  element = xml.getElementByIndex(loop);

	 if(element!=null)
	 {
	  DataNode newNode = new DataNode(engine,this.parent);
	  newNode.buildFromgrfxML( element );
	  try{
	  	add(newNode,this);
	  }
	  catch(LockException le){}
	 }
	  /*try{*/
		/*Class clazz  = Class.forName(NameHelper.toClassName(element.getName()) );
		Constructor constructor = clazz.getConstructor( new Class[]{Class.forName("org.one.stone.soup.grfxML.GrfxMLEngine")} );
		DataNode newItem = (DataNode)constructor.newInstance( new Object[]{engine} );*/

		//DataNode newItem = (DataNode)clazz.newInstance();
		/*clazz.
			getMethod(NameHelper.BUILD_METHOD,NameHelper.BUILD_SIG).
				invoke(newItem,new Object[]{element});
		add(newItem,this);*/
	  /*}
	  catch(ClassNotFoundException ce)
	  {
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{element.getName(),Token.CHILDREN,""+element.getStartLineNo()} );
	  }
	  catch(Exception e)
	  {
		System.out.println("Error in "+NameHelper.toClassName(element.getName()));
		e.printStackTrace();
	  }// Major exception handleing needed here (and in this function)
	 }*/

	}

}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	int loop;

	for(loop=0;loop<xml.getElementCount();loop++)
	{
	  element = xml.getElementByIndex(loop);

	 if(element!=null)
	 {
	  if(loop<size())
	  {
		  get(loop).copyFromgrfxML( element );
	  }
	  else
	  {
		  try{
			Class clazz  = Class.forName(NameHelper.toClassName(element.getName()) );
			DataNode newItem = (DataNode)clazz.newInstance();
			clazz.
				getMethod(NameHelper.BUILD_METHOD,NameHelper.BUILD_SIG).
					invoke(newItem,new Object[]{element});
			add(newItem,this);
		  }
		  catch(ClassNotFoundException ce)
		  {
				GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{element.getName(),Token.CHILDREN,""+element.getStartLineNo()} );
		  }
		  catch(Exception e)
		  {
			e.printStackTrace();
		  }// Major exception handleing needed here (and in this function)
		 }
	 }
	}

	while(loop<size())
	{
		try{
			remove( size()-1,this );
		}catch(LockException le){}
	}
}
public DataNode get(int index)
{
	return (DataNode)_get(index);
}
public DataPoint getBounds() {
	return _bounds;
}
public DataChildren getClone() {
	DataChildren newChildren = new DataChildren(engine,parent);

	for(int loop=0;loop<size();loop++)
	{
	  try{
		newChildren.add(
				((DataNode)get(loop)).getClone(),
				this
			);
	  }
	  catch(LockException le){}// dummy catch
	}

	return newChildren;
}
public DataChildren getClone(String path) {

	DataChildren newChildren = new DataChildren(engine,parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newChildren );

	for(int loop=0;loop<size();loop++)
	{
	  try{
		newChildren.add(
				((DataNode)get(loop)).getClone(path),
				this
			);
	  }
	  catch(LockException le){}// dummy catch
	}

	return newChildren;
}
public String getMemberName() {
	return Token.NODE;
}
public void insert(DataNode data,int index,Object source) throws LockException
{
	_insert(data,index,source);
}
public void register(DataLinker store) {
	super.register(store);
}
public void remove(int index,Object source) throws LockException
{
	_remove(index,source);
}
public void remove(DataNode data,Object source) throws LockException
{
	_remove(data,source);
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj, boolean recursive) {

	if(oldObj==parent)
	{
		parent = (DataNode)newObj;
	}

	for (int loop = 0; loop < size(); loop++) {
		if (get(loop) == oldObj) {
			try {
				set((DataNode) newObj, loop, this);
			} catch (LockException le) {
			} //dummy catch
		}
		get(loop).replace(oldObj, newObj, recursive);
	}
}
public void resetChanged() {
	super.resetChanged();
}
public void setParent(DataNode newParent) {
	parent = newParent;
	for (int loop = 0; loop < size(); loop++) {
		get(loop).setParent(parent);
	}
}
public void set(DataNode data,int index,Object source) throws LockException
{
	_set(data,index,source);
}
public void setBounds(DataPoint newBounds) {
	_bounds = newBounds;
}
public String togrfxML(String name) {

	String xml = "<"+name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";

	for (int loop = 0; loop < size(); loop++) {
		try {
			xml += get(loop).togrfxML(NameHelper.systemToTagName((String) get(loop).getClass().getField(Token.DEFAULT_ID).get(get(loop))));
		} catch (Exception e) {
		} //dummy catch

	}

	xml+="</"+name+">\n";

	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1).toLowerCase();

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml+="/>";

	return xml;
}
public void translate(DataTranslation t)
{
	if(size()==0)
		return;
	// translate each of the child nodes
	for(int loop=size()-1;loop>=0;loop--)
	{
		get(loop).translate(t);
	}

	DataPoint checkOffset;
 	DataPoint check;
	int w,mw=0;
	int h,mh=0;

	for (int loop = 0; loop < size(); loop++) {
		try {

			check = get(loop).getBounds();
			checkOffset = get(loop).getTranslation().getPosition();
			w = check.getValueX() + checkOffset.getValueX();
			h = check.getValueY() + checkOffset.getValueY();

			if(w>mw)
				mw=w;
			if(h>mh)
				mh=h;
		} catch (Exception e) {
		} //dummy catch

	}
	try{
		_bounds.setValueX(mw,this);
		_bounds.setValueY(mh,this);
	}
	catch(LockException le){}//dummy catch

}
}
